package net.melon.uploader.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.esj.basic.utils.writer.WriteException;
import net.esj.basic.utils.writer.json.JsonWriter;
import net.melon.uploader.UploadCounter;
import net.melon.uploader.Uploader;

public class UploadCounterServlet extends HttpServlet {

	private JsonWriter jsonWriter = new JsonWriter();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		UploadCounter uc = (UploadCounter) req.getSession().getAttribute(Uploader.SYS_UPLOAD_COUNTER_SESSION_KEY);
		PrintWriter writer = resp.getWriter();
		try {
			resp.setContentType("text/plain;charset=UTF-8");
			writer.write(jsonWriter.write(uc));
		} catch (WriteException e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				writer.close();
			}
		}
	}

	
}
